package com.application.item.manage;

import java.io.File;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.application.item.pojo.Car;
import com.application.item.pojo.Item;
import com.application.item.pojo.Part;

 


public class ItemManage {
	private static Logger logger = Logger.getLogger(ItemManage.class);
	static Document document;
	static XPath xpath;
 
	static HashMap<Integer, Item> allItems = new HashMap<Integer, Item>();
	public static HashMap<Integer, Item> items = new HashMap<Integer, Item>();
	public static HashMap<Integer, Item> parts = new HashMap<Integer, Item>();
	public static HashMap<Integer, Item> cars = new HashMap<Integer, Item>();
	private static final String ITEMDATA_FILE_PATH = "res/item/";

	
	
 
	public static void init() {
		//log.info("init items start ............");
		loadItems("items.xml", items, "/items", 0);
		loadItems("parts.xml", parts, "/parts", 1);
		loadItems("cars.xml", cars, "/cars", 2);
	}
	private static void loadItems(String url, HashMap <Integer, Item>map, String yu, int type) {
		try {

			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			logger.info("init items " + yu);
			document = builder.parse(new File(ITEMDATA_FILE_PATH+url));

			xpath = XPathFactory.newInstance().newXPath();
			if (type == 2)
				loadCars(map, yu, type);
			else if (type == 1)
				loadParts(map, yu, type);
			else
				loadItems(map, yu, type);
			logger.info("init items " + yu + " end ........");

		} catch (Exception e) {
			e.printStackTrace();

			logger.info(e.getStackTrace().toString());
		}
	}

	private static void loadItems(HashMap<Integer, Item> map, String expression, int type)
			throws Exception {
		Node mapRoot = (Node) xpath.evaluate(expression, document,
				XPathConstants.NODE);
		NodeList list = mapRoot.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node item = list.item(i);
			if (item.getNodeName().equals("item")) {
				NodeList proplist = item.getChildNodes();
				Item baseItem = new Item();
				baseItem.itemType = type;
				for (int j = 0; j < proplist.getLength(); j++) {
					Node prop = proplist.item(j);

					if (prop.getNodeName().equals("id")) {
						baseItem.itemId = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("pile")) {
						baseItem.maxPileNum = Integer
								.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("price")) {
						baseItem.saleValue = Integer.parseInt(prop
								.getTextContent());
					}else if (prop.getNodeName().equals("sale")) {
						baseItem.saleEnable = (Integer.parseInt(prop
								.getTextContent())==1)?true:false;
					}

				}
				map.put(baseItem.itemId, baseItem);
				allItems.put(baseItem.itemId, baseItem);
				logger.info(Integer.toString(baseItem.itemId));

			}

		}

	}

	private static void loadCars(HashMap<Integer, Item> map, String expression, int type)
			throws Exception {
		Node mapRoot = (Node) xpath.evaluate(expression, document,
				XPathConstants.NODE);
		NodeList list = mapRoot.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node item = list.item(i);
			if (item.getNodeName().equals("item")) {
				NodeList proplist = item.getChildNodes();
				Car car = new Car();
				car.itemType = type;
				for (int j = 0; j < proplist.getLength(); j++) {
					Node prop = proplist.item(j);

					if (prop.getNodeName().equals("id")) {
						car.itemId = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("pile")) {
						car.maxPileNum = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("price")) {
						car.saleValue = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("endure")) {
						car.endure = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("speed")) {
						car.speed = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("acccft")) {
						car.acccft = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("bracft")) {
						car.bracft = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("sccft")) {
						car.sccft = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("turncft")) {
						car.turncft = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("confradius")) {
						car.confradius = Integer
								.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("maxatk")) {
						car.maxatk = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("minatk")) {
						car.minatk = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("maxdef")) {
						car.maxdef = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("mindef")) {
						car.mindef = Integer.parseInt(prop.getTextContent());
					} else if (prop.getNodeName().equals("fatalatkchance")) {
						car.fatalatkchance = Integer.parseInt(prop
								.getTextContent());
					} else if (prop.getNodeName().equals("unfatalatkchance")) {
						car.unfatalatkchance = Integer.parseInt(prop
								.getTextContent());
					} else if (prop.getNodeName().equals("dodgechance")) {
						car.dodgechance = Integer.parseInt(prop
								.getTextContent());
					} else if (prop.getNodeName().equals("undodgechance")) {
						car.undodgechance = Integer.parseInt(prop
								.getTextContent());
					} else if (prop.getNodeName().equals("caroticchance")) {
						car.caroticchance = Integer.parseInt(prop
								.getTextContent());
					} else if (prop.getNodeName().equals("uncaroticchance")) {
						car.uncaroticchance = Integer.parseInt(prop
								.getTextContent());
					} else if (prop.getNodeName().equals("weight")) {
						car.weight = Integer.parseInt(prop.getTextContent());
					}else if (prop.getNodeName().equals("sale")) {
						car.saleEnable = (Integer.parseInt(prop
								.getTextContent())==1)?true:false;
					}

				}
				map.put(car.itemId, car);
				allItems.put(car.itemId, car);
			}

		}

	}
	private static void loadParts(HashMap <Integer, Item>map, String expression, int type)
	throws Exception {
	Node mapRoot = (Node) xpath.evaluate(expression, document,
			XPathConstants.NODE);
	NodeList list = mapRoot.getChildNodes();
	for (int i = 0; i < list.getLength(); i++) {
		Node item = list.item(i);
		if (item.getNodeName().equals("item")) {
			NodeList proplist = item.getChildNodes();
			Part car = new Part();
			car.itemType = type;
			for (int j = 0; j < proplist.getLength(); j++) {
				Node prop = proplist.item(j);
				if (prop.getNodeName().equals("pt")) {
					car.pt = (byte)Integer.parseInt(prop.getTextContent());
				}else if (prop.getNodeName().equals("id")) {
					car.itemId = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("pile")) {
					car.maxPileNum = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("price")) {
					car.saleValue = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("endure")) {
					car.endure = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("speed")) {
					car.speed = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("acccft")) {
					car.acccft = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("bracft")) {
					car.bracft = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("sccft")) {
					car.sccft = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("turncft")) {
					car.turncft = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("confradius")) {
					car.confradius = Integer
							.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("maxatk")) {
					car.maxatk = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("minatk")) {
					car.minatk = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("maxdef")) {
					car.maxdef = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("mindef")) {
					car.mindef = Integer.parseInt(prop.getTextContent());
				} else if (prop.getNodeName().equals("fatalatkchance")) {
					car.fatalatkchance = Integer.parseInt(prop
							.getTextContent());
				} else if (prop.getNodeName().equals("unfatalatkchance")) {
					car.unfatalatkchance = Integer.parseInt(prop
							.getTextContent());
				} else if (prop.getNodeName().equals("dodgechance")) {
					car.dodgechance = Integer.parseInt(prop
							.getTextContent());
				} else if (prop.getNodeName().equals("undodgechance")) {
					car.undodgechance = Integer.parseInt(prop
							.getTextContent());
				} else if (prop.getNodeName().equals("caroticchance")) {
					car.caroticchance = Integer.parseInt(prop
							.getTextContent());
				} else if (prop.getNodeName().equals("uncaroticchance")) {
					car.uncaroticchance = Integer.parseInt(prop
							.getTextContent());
				} else if (prop.getNodeName().equals("weight")) {
					car.weight = Integer.parseInt(prop.getTextContent());
				}else if (prop.getNodeName().equals("sale")) {
					car.saleEnable = (Integer.parseInt(prop
							.getTextContent())==1)?true:false;
				}
	
			}
			map.put(car.itemId, car);
			allItems.put(car.itemId, car);
		}
	
	}
	
	}
	public static Item getItem(int itemId,String order) {
		if (allItems.containsKey(itemId)) {
			return allItems.get(itemId);
		}
		logger.error("bad item id =" + itemId + "  order =" + order);
		return null;
	}
 
 

}
